<style type="text/css">
    .action-cell {
        margin-bottom: 20px;
        width: 100%;
        display: -webkit-box;
        display: -ms-flexbox;
        display: flex;
        -ms-flex-pack: distribute;
        justify-content: space-between;
        align-items: center;
        margin-right: 20%;
    }

    .search-block {
        display: flex;
        margin-bottom: 10px;
    }

    .input-search {
        margin-right: 10px;
    }
</style>
<template>
    <div style="min-width: 1024px">
        <div class="action-cell">
            <div>
                <h4>车辆用途管理</h4>
                <Alert show-icon>
                    此处用于车辆用途列表管理。
                </Alert>
                <ButtonGroup>
                    <Button type="success" @click="jumpPage('','CarPublicAdd')">
                        <Icon type="md-add-circle"/>
                        添加
                    </Button>
                    <Button type="warning" :disabled="deleteBtnEnable" @click="remove">
                        <Icon type="md-close-circle"/>
                        删除
                    </Button>
                </ButtonGroup>
            </div>
            <div class="search-block">
                <i-input v-model="searchUserInput" placeholder="根据车牌号搜索">
                    <i-button slot="append" @click="searchEvent()" icon="ios-search"></i-button>
                </i-input>
            </div>
        </div>
        <Row>
            <Col span="20">
                <Table :context="self" :data="tableData" :columns="tableColumns" stripe border
                       @on-select="tableItemChecked" @on-select-cancel="tableItemChecked"
                       @on-select-all="tableItemChecked"
                       @on-select-all-cancel="tableItemChecked"
                       style="margin-top:15px;">
                </Table>
            </Col>
        </Row>
        <div style="margin: 10px;overflow: hidden">
            <div style="float: right;">
                <Page :total="total" size="large" show-total :current="current" show-elevator show-sizer
                      @on-change="changePage"></Page>
            </div>
        </div>
    </div>
</template>
<script>
    import apiUtil from '../../assets/utils/api_util';

    export default {
        data() {
            return {
                self: this,
                //操作按钮状态管理
                deleteBtnEnable: true,
                selectedItems: [],
                tableData: [],
                total: 0,
                current: 1,
                searchUserInput:'',
                //搜索
                searchText: '',
                tableColumns: [
                    {
                        type: 'selection',
                        width: 60,
                        align: 'center'
                    },
                    {
                        title: '车牌号',
                        key: 'Carcode',
                        align: 'center',
                    },
                    {
                        title: '用途',
                        key: 'Cartype',
                        align: 'center',
                    },
                    {
                        title: '车辆品牌',
                        key: 'Brand',
                        align: 'center',
                    },
                    {
                        title: '车辆颜色',
                        key: 'Color',
                        align: 'center',
                    },
                    {
                        title: '车辆车架号',
                        key: 'Carsn',
                        align: 'center',
                    },
                    {
                        title: '车辆卡编号',
                        key: 'Cardid',
                        align: 'center',
                    },
                    {
                        title: '状态',
                        key: 'State',
                        align: 'center',
                    },
                    {
                        title: '操作',
                        key: 'action',
                        width: 150,
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [
                                h('Button', {
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        'margin-right': '10px',
                                    },
                                    on: {
                                        click: () => {
                                            let that = this;
                                            this.jumpPage(params.row.Carcode, 'CarPublicEdit');
                                        }
                                    }
                                }, '编辑'),
                            ])
                        }
                    }
                ]
            }
        },
        mounted() {
            this.mockTableData();
        },

        methods: {
            //跳转
            jumpPage: function (id, url) {
                //跳转
                let route = '';
                if (id.length > 0) {
                    route = {
                        path: url,
                        query: {
                            id: id,
                        }
                    }

                } else {
                    route = {
                        path: url,
                    };
                }

                this.$router.push(route);
            },
            mockTableData() {
                let that = this;

                let para = {
                    currPage: this.current,
                    pageSize: 10,
                };
                let params = that.$qs.stringify(para);

                this.$Loading.start();

                apiUtil.get(that, apiUtil.urls.CarList, para, function (res) {
                    that.$Loading.finish();
                    that.total = res.Msg.TotalPage;
                    that.current = res.Msg.CurrentPage;
                    that.tableData = res.Msg.Arrays;

                    console.log('tableData:', res)
                });

            },
            // 表格内选中及取消选中操作
            tableItemChecked: function (selection, row) {

                this.selectedItems = selection;
                console.log('选择删除项:', selection);

                if (this.selectedItems.length > 1) {
                    // 此处添加不要多选的提示
                    this.$Message.warning('此处不允许批量删除 ');
                    this.deleteBtnEnable = true;

                } else {
                    this.deleteBtnEnable = false;
                }
            },
            //删除
            remove: function () {
                let self = this;
                let that = this;
                this.$Modal.confirm({
                    title: '操作警告',
                    content: '即将删除选中的车辆，该操作不可逆转，请确认您的操作。',
                    onOk: function () {
                        this.$Loading.start();
                        var selectedItemIds = "";
                        for (var i = 0; i < that.selectedItems.length; i++) {
                            selectedItemIds += that.selectedItems[i].Id.toString() + ",";
                        }
                        selectedItemIds = selectedItemIds.substring(0, selectedItemIds.length - 1);
                        let para = {id: selectedItemIds};
                        let params = that.$qs.stringify(para);

                        apiUtil.post(that, apiUtil.urls.DelCarInfo, params, function (res) {
                            that.$Message.success('删除成功。');
                            that.mockTableData();
                        });
                    }
                });
            },
            changePage(row) {
                // 这里直接更改了模拟的数据，真实使用场景应该从服务端获取数据
                console.log("row", row);
                this.current = row;
                this.mockTableData();
            },

            searchEvent: function () {
                let that = this;
                let para = {
                    keyWords: that.searchUserInput,
                    currPage: this.current,
                    pageSize: 10,
                }
                let params = that.$qs.stringify(para);

                apiUtil.get(that, apiUtil.urls.CarList, para, function (res) {

                    that.tableData = res.Msg.Arrays;
                });
            }
        },
    }
</script>
